//
// Created by lenovo on 2024/5/27.
//

#include "law.h"
bool cell_status (vector<vector<bool> >world , int x , int y){
    int neighbors = 0 ;
//    cout << world.size() << ' ' << world.at(0).size() << endl ;
    for (int i = x - 1 ; i <= x + 1 ; i ++){
        for (int j = y - 1 ; j <= y + 1 ; j ++){
//            cout << "(" << i << "," << j << ")" << "\t" << world[i][j] << endl ; ;
            if (i < 0)
                continue;
            if (j < 0)
                continue;
            if (i == world.size() or j == world.at(0).size())
                continue;
            if (i == x and j == y)
                continue;

            if (world[i][j]){
//                cout << "\t is alive" << endl ;
                neighbors++ ;

            }

        }
    }

    if (world[x][y]){
        //alive
        if (neighbors < 2)
            return 0 ;
        else if (neighbors > 3)
            return 0 ;
        else
            return 1 ;
    }
    else{
//        cout << "prev died cell" << endl ;
//        cout << "the neighbor_cnt is " << neighbors << endl ;
        //died
        if (neighbors == 3)
            return 1 ;
        else
            return 0 ;
    }
}

vector<vector<bool> > update (vector<vector<bool> > prev){
    vector<vector<bool> > post (
            prev.size() ,
            vector<bool> (prev.at(0).size())
    );

    for (int x = 0 ; x < prev.size() ; x ++){
        for (int y = 0 ; y < prev.at(0).size() ; y ++){
            post[x][y] = cell_status(prev , x , y);
        }
    }
    return post ;
}